﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using WITS.Framework.Event;
using WITS.Framework.Common;
using WITS.Framework.Exceptions;
using System.Web;

namespace WITS.Framework.WebPage
{
    public class WebPageBase : Page
    {
        bool _isReady = false;
        public WebPageBase()
        {
            _isReady = false;
        }
        /// <summary>
        /// Request已经重新定义，不放开使用
        /// </summary>
        public new HttpRequest Request
        {
            get
            {
                if (!_isReady)
                    return base.Request;
                else
                    return null;
            }
        }
        /// <summary>
        /// Response已经重新定义，不放开使用
        /// </summary>
        public HttpResponse Reponse
        {
            get
            {
                if (!_isReady)
                    return base.Response;
                else
                    return null;
            }
        }
        /// <summary>
        /// 此属性提供原Page的Request以供做PageBase使用，继承类请将该方法屏蔽
        /// </summary>
        protected HttpRequest BaseRequest
        {
            get
            {
                return base.Request;
            }
        }

        /// <summary>
        /// 获取Request的参数
        /// </summary>
        public Dictionary<string, string> RequestParameters
        {
            get
            {
                return (Dictionary<string, string>)ViewState[this.ClientID + "_RequestStrings"];
            }
        }
        /// <summary>
        /// 连接跳转
        /// </summary>
        /// <param name="url"></param>
        public void Redirect(string url)
        {
            if (GlobalCommon.SecurityStrategy.IsValidUrl(url))
            {
                Response.Redirect(HttpUtility.HtmlEncode(url));
            }
            else
            {
                throw new BizException(url + ":Is not valid URL");
            }
        }

        protected sealed override void OnLoad(EventArgs e)
        {
            try
            {
                //屏蔽Request的开关
                _isReady = true;
                //获取request参数
                Dictionary<string, string> _requeststrings = new Dictionary<string, string>();
                foreach (string s in base.Request.QueryString.Keys)
                {
                    _requeststrings.Add(s, HttpUtility.HtmlEncode(Request[s]));
                }
                ViewState.Remove(this.ClientID + "_RequestStrings");
                ViewState.Add(this.ClientID + "_RequestStrings", _requeststrings);
                OnPageLoad();
                FindControls(this);
                base.OnLoad(e);
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
            }
        }

        protected sealed override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
        {
            try
            {
                base.RaisePostBackEvent(sourceControl, eventArgument);
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
            }
        }

        public virtual void OnPageLoad()
        {
            //do nothing
        }

        protected sealed override void OnInit(EventArgs e)
        {
            try
            {
                base.OnInit(e);
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
            }
           
        }
        public sealed override void ProcessRequest(System.Web.HttpContext context)
        {
            try
            {
                base.ProcessRequest(context);
            }
            catch (Exception ex)
            {
               GlobalCommon.ExceptionProcessor.DoProcess(ex);
            }

        }
        Dictionary<string, Control> _controls = new Dictionary<string, Control>();
        /// <summary>
        /// 搜索頁面裏面的控件并添加到字典中
        /// </summary>
        /// <param name="contrainer"></param>
        public virtual void FindControls(Control contrainer)
        {
            if (contrainer is ContentPlaceHolder)
            {
                foreach (Control c in contrainer.Controls)
                {
                    if (c is TextBox)
                    {
                        _controls.Add(c.ID, c);
                    }
                    if (c is Label)
                    {
                        _controls.Add(c.ID, c);
                    }
                    if (c is DropDownList)
                    {
                        _controls.Add(c.ID, c);
                    }
                    if (c is RadioButton)
                    {
                        _controls.Add(c.ID, c);
                    }
                    if (c is RadioButtonList)
                    {
                        _controls.Add(c.ID, c);
                    }
                }
            }
            else
            {
                foreach (Control c in contrainer.Controls)
                {
                    FindControls(c);
                }
            }
        }
        /// <summary>
        /// 根據PageMapping獲取對應的數據Module
        /// </summary>
        /// <param name="m">APageMapping</param>
        /// <returns>PageModule</returns>
        public virtual PageModule GetModule(APageMapping m)
        {
            PageModule rtn = new PageModule();

            foreach (string cn in m.PageToManageMapping.Keys)
            {
                if (_controls.ContainsKey(m.PageToManageMapping[cn]))
                {
                    if (_controls[m.PageToManageMapping[cn]] is TextBox)
                    {
                        rtn.SetValue(cn, ((TextBox)_controls[m.PageToManageMapping[cn]]).Text);
                    }
                    else if (_controls[m.PageToManageMapping[cn]] is Label)
                    {
                        rtn.SetValue(cn, ((Label)_controls[m.PageToManageMapping[cn]]).Text);
                    }
                    else if (_controls[m.PageToManageMapping[cn]] is DropDownList)
                    {
                        rtn.SetValue(cn, ((DropDownList)_controls[m.PageToManageMapping[cn]]).SelectedValue);
                    }
                    else if (_controls[m.PageToManageMapping[cn]] is RadioButton)
                    {
                        rtn.SetValue(cn, ((RadioButton)_controls[m.PageToManageMapping[cn]]).Checked);
                    }
                    else if (_controls[m.PageToManageMapping[cn]] is RadioButtonList)
                    {
                        rtn.SetValue(cn, ((RadioButtonList)_controls[m.PageToManageMapping[cn]]).SelectedValue);
                    }
                }
            }

            return rtn;
        }
        /// <summary>
        /// 根據PageMapping獲取對應的數據Module
        /// </summary>
        /// <param name="m">APageMapping</param>
        /// <param name="pm">PageModule</param>
        public virtual void SetModuleValue(APageMapping m, PageModule pm)
        {
            foreach (string cn in m.ManageToPageMapping.Keys)
            {
                if (_controls[m.PageToManageMapping[cn]] is TextBox)
                {
                   ((TextBox)_controls[m.ManageToPageMapping[cn]]).Text = pm.GetValue<string>(cn);
                }
                else if (_controls[m.PageToManageMapping[cn]] is Label)
                {
                    ((Label)_controls[m.ManageToPageMapping[cn]]).Text = pm.GetValue<string>(cn);
                }
                else if (_controls[m.PageToManageMapping[cn]] is DropDownList)
                {
                    ((DropDownList)_controls[m.ManageToPageMapping[cn]]).SelectedValue = pm.GetValue<string>(cn);
                }
                else if (_controls[m.PageToManageMapping[cn]] is RadioButton)
                {
                    ((RadioButton)_controls[m.ManageToPageMapping[cn]]).Checked = pm.GetValue<bool>(cn);
                }
                else if (_controls[m.PageToManageMapping[cn]] is RadioButtonList)
                {
                    ((RadioButtonList)_controls[m.ManageToPageMapping[cn]]).SelectedValue = pm.GetValue<string>(cn);
                }

            }

        }
    }
}
